# Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have included with
# MySQL.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/C++, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

# List of static libraries to be merged

SET(STATIC_LIBS "@STATIC_LIBS@")

# Work location where libraries are merged. For each
# library LIB from STATIC_LIBS, this location should
# contain the library archive at ${MERGELIBS_DIR}/${LIB}/lib.a

SET(MERGELIBS_DIR "@MERGELIBS_DIR@")

# Othert settings

SET(CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@")
SET(CMAKE_AR "@CMAKE_AR@")
SET(CMAKE_RANLIB "@CMAKE_RANLIB@")
set(LIBTOOL_COMMAND "@LIBTOOL_COMMAND@")

#message("mergelib script: ${MERGELIBS_DIR}")


file(REMOVE ${TARGET_LOCATION})

if(APPLE)

  # On OSX we use libtool which can merge static libraries

  set(merge_llibs)
  foreach(lib ${STATIC_LIBS})
    list(APPEND merge_libs ${MERGELIBS_DIR}/${lib}/lib.a)
  endforeach()

  execute_process(
    COMMAND ${LIBTOOL_COMMAND} -static -o ${TARGET_LOCATION}
      ${merge_libs}
    ERROR_QUIET
  )

else()

  # Otherwise we use ar to first unppack objects from all archives
  # and then pack them back into a single static library.

  file(MAKE_DIRECTORY ${MERGELIBS_DIR}/objs)

  set(merge_objs)

  foreach(lib ${STATIC_LIBS})

    # Get list of objects in the archive

    execute_process(
      COMMAND ${CMAKE_AR} -t ${lib}/lib.a
      WORKING_DIRECTORY ${MERGELIBS_DIR}
      OUTPUT_VARIABLE objs
    )

    #message("extracting objs from ${lib}: ${objs}")

    # split output into cmake list

    string(REGEX MATCHALL "[^\n]+" objs ${objs})

    #
    # Extract all objects and rename them, making sure that the resuliting
    # names will be unique across the output library. This is done by adding
    # library name prefix to each object comming from that library.
    #

    file(MAKE_DIRECTORY ${MERGELIBS_DIR}/${lib}/objs)

    foreach(obj ${objs})

      # extract object from the library ...

      execute_process(
        COMMAND ar x ../lib.a ${obj}
        WORKING_DIRECTORY ${MERGELIBS_DIR}/${lib}/objs
      )

      # ... and rename it

      file(RENAME
        ${MERGELIBS_DIR}/${lib}/objs/${obj}
        ${MERGELIBS_DIR}/objs/${lib}_${obj}
      )

      list(APPEND merge_objs
        ${MERGELIBS_DIR}/objs/${lib}_${obj}
      )

    endforeach()

  endforeach()

  execute_process(
    COMMAND ${CMAKE_AR} -r ${TARGET_LOCATION} ${merge_objs}
  )
  execute_process(
    COMMAND ${CMAKE_RANLIB} ${TARGET_LOCATION}
  )

endif()

#message("mergelibs script done")


